package com.cyzy.service.impl;

import com.cyzy.mapper.OrderMapper;
import com.cyzy.mapper.UserMapper;
import com.cyzy.pojo.TrainOrder;
import com.cyzy.pojo.TrainUser;
import com.cyzy.pojo.UserOrder;
import com.cyzy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    //查询订单列表
    @Override
    public List<UserOrder> selectOrder(long userId) {
        return orderMapper.selectOrder(userId);
    }

    //取消订单
    @Override
    public int cancelOrder(TrainOrder trainOrder) {
        trainOrder.setOrderStatus("已取消");
        return orderMapper.cancelOrder(trainOrder);
    }

    //删除订单
    @Override
    public int deleteOrder(TrainOrder trainOrder) {
        trainOrder.setOrderStatus("已删除");

        return orderMapper.deleteOrder(trainOrder);
    }

    //查询订单
    @Override
    public TrainOrder selectOrderById(long orderId) {
        return orderMapper.selectOrderById(orderId);
    }


    //余额支付
    @Override
    public int payBalance(TrainOrder trainOrder) {
        TrainOrder order = orderMapper.selectOrderById(trainOrder.getOrderId());
        System.out.println(trainOrder.getOrderId()+"===============================");
        long money2 = order.getorderMoney();
        TrainUser user = userMapper.selectById(order.getUserId());
        long money1 = user.getUserMoney();
        if (money1 < money2) {
            return 0;
        }
        long userMoney = money1 - money2;
        userMapper.userMoney(user.getUserId(),userMoney);
        trainOrder.setOrderStatus("已付款");
        return  orderMapper.payBalance(trainOrder);
    }


}
